﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUL;
using System.Data;
namespace Hospital
{
    public partial class ManagerStaff : System.Web.UI.Page
    {
        //DAO
        StaffDAO staffDAO = new StaffDAO();
        QualificationDAO quaDAO = new QualificationDAO();
        ExperienceDAO exDAO = new ExperienceDAO();
        //BUL
        StaffBUL staffBUL = new StaffBUL();
        QualificationBUL quaBUL = new QualificationBUL();
        ExperienceBUL exBUL = new ExperienceBUL();
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            grvStaff.DataSource = staffBUL.load();
            grvStaff.DataBind();
            listPosition.Items.Add("Bác sĩ");
            listPosition.Items.Add("Y tá");
            listGender.Items.Add("Nam");
            listGender.Items.Add("Nữ");
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            //insert qualification
            quaDAO.Type = txtType.Text;
            quaDAO.Institution = txtInstitution.Text;
            quaDAO.Date = DateTime.Parse(txtDate.Text);
            quaDAO.Date.ToString("dd / MM / yyyy");
            quaBUL.insert(quaDAO);
            //insert experience
            exDAO.Position = txtPo.Text;
            exDAO.StartDate = DateTime.Parse(txtStart.Text);
            exDAO.StartDate.ToString("dd/mm/yyyy");
            exDAO.FinishDate = DateTime.Parse(txtFinish.Text);
            exDAO.FinishDate.ToString("dd/mm/yyyy");
            exDAO.Organization = txtOrganization.Text;
            exBUL.insert(exDAO);
            //insert staff);
            staffDAO.Address = txtAddress.Text;
            staffDAO.Birthday = DateTime.Parse(txtBirth.Text);
            staffDAO.Birthday.ToString("dd/mm/yyyy");
            staffDAO.Fullname = txtName.Text;
            staffDAO.Gender = listGender.Text;
            staffDAO.IdExperience = exBUL.getIdLast();
            staffDAO.IdQualification = quaBUL.getIdLast();
            staffDAO.Position = listPosition.Text;
            staffDAO.Salary = Int32.Parse(txtSalary.Text);
            staffDAO.Phone = txtPhone.Text;
            staffBUL.insert(staffDAO);
            Page_Load(sender, e);
            reset();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if(!txtId.Text.Equals("")){
                //delete qualification
                quaBUL.delete(quaBUL.getIdByStaff(txtId.Text).ToString());
                //delete experience
                exBUL.delete(exBUL.getIdByStaff(txtId.Text).ToString());
                //delete staff
                staffBUL.delete(txtId.Text);
                reset();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //insert staff);
            staffDAO.Address = txtAddress.Text;
            staffDAO.Birthday = DateTime.Parse(txtBirth.Text);
            staffDAO.Birthday.ToString("dd / MM / yyyy");
            staffDAO.Fullname = txtName.Text;
            staffDAO.Gender = listGender.Text;
            staffDAO.Position = listPosition.Text;
            staffDAO.Salary = Int32.Parse(txtSalary.Text);
            staffDAO.Phone = txtPhone.Text;
            staffDAO.IdStaff = Int32.Parse(txtId.Text);
            staffBUL.update(staffDAO);
            //update qualification
            quaDAO.Type = txtType.Text;
            quaDAO.Institution = txtInstitution.Text;
            quaDAO.Date = DateTime.Parse(txtDate.Text);
            quaDAO.Date.ToString("dd / MM / yyyy");
            quaDAO.IdQualification = quaBUL.getIdByStaff(txtId.Text);
            quaBUL.update(quaDAO);
            //insert experience
            exDAO.Position = txtPo.Text;
            exDAO.StartDate = DateTime.Parse(txtStart.Text);
            exDAO.StartDate.ToString("dd / MM / yyyy");
            exDAO.FinishDate = DateTime.Parse(txtFinish.Text);
            exDAO.FinishDate.ToString("dd / MM / yyyy");
            exDAO.Organization = txtOrganization.Text;
            exDAO.IdExperience = exBUL.getIdByStaff(txtId.Text);
            exBUL.update(exDAO);
            reset();
            Page_Load(sender, e);
            
        }

        protected void txtReset_Click(object sender, EventArgs e)
        {
            reset();
        }
        private void reset() {
            //select staff
            txtId.Text = "";
            txtName.Text = "";
            txtBirth.Text = "";
            txtAddress.Text = "";
            // txtPo.Text = grvStaff.SelectedRow.Cells[5].Text;
            //listGender.Text = grvStaff.SelectedRow.Cells[6].Text;
            txtPhone.Text = "";
            txtSalary.Text = "";
            //select qualifi
            dt = quaBUL.load(txtId.Text);
            txtType.Text = "";
            txtDate.Text = "";
            txtInstitution.Text = "";
            dt.Clear();
            //select ex
            dt = exBUL.load(txtId.Text);
            txtPo.Text = "";
            txtStart.Text = "";
            txtFinish.Text = "";
            txtOrganization.Text = "";
        }
        protected void grvStaff_SelectedIndexChanged(object sender, EventArgs e)
        {
            //select staff
            txtId.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[1].Text);
            txtName.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[2].Text);
            txtBirth.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[3].Text);
            txtAddress.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[4].Text);
           // txtPo.Text = grvStaff.SelectedRow.Cells[5].Text;
            listGender.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[6].Text);
            txtPhone.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[7].Text);
            txtSalary.Text =HttpUtility.HtmlDecode(grvStaff.SelectedRow.Cells[8].Text);
            //select qualifi
            dt = quaBUL.load(txtId.Text);
            txtType.Text =  dt.Rows[0][0].ToString();
            txtDate.Text = dt.Rows[0][1].ToString();
            txtInstitution.Text = dt.Rows[0][2].ToString();
            dt.Clear();
            //select ex
            dt = exBUL.load(txtId.Text);
            txtPo.Text = dt.Rows[0][0].ToString();
            txtStart.Text = dt.Rows[0][1].ToString();
            txtFinish.Text = dt.Rows[0][2].ToString();
            txtOrganization.Text = dt.Rows[0][3].ToString();
            Page_Load(sender, e);
        }
    }
}